package com.mobis.spit.controller;


import com.mobis.spit.pojo.Spit;
import com.mobis.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/spit")
public class SpitController {

    private  static Logger logger= LoggerFactory.getLogger(SpitController.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    public SpitService spitService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result findAllSpit(){
        List<Spit> list = spitService.findAll();
        return new Result(true, StatusCode.OK,"查找全部成功",list);
    }

    @RequestMapping(value = "/{spitId}",method = RequestMethod.GET)
    public Result findSpitById(@PathVariable String spitId){
        logger.info("开始查询单个spit");
        Spit spit=spitService.findSpitById(spitId);
        return new Result(true,StatusCode.OK,"查找一个成功",spit);
    }

    @RequestMapping(value = "/{spitId}",method = RequestMethod.PUT)
    public Result updateSpit(@PathVariable String spitId, @RequestBody Spit spit){
        spit.set_id(spitId);
        spitService.update(spit);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @RequestMapping(value = "/{spitId}",method = RequestMethod.DELETE)
    public Result deleteSpitById(@PathVariable String spitId){

        spitService.deleteBySpit(spitId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public Result AddSpit(@RequestBody Spit spit){
        logger.info("开始添加spit");
        spitService.addSpid(spit);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @RequestMapping(value = "/comment/{parentid}/{page}/{size}",method = RequestMethod.GET)
    public Result findByParentId(@PathVariable  String parentid, @PathVariable  Integer page, @PathVariable Integer size){
        logger.info("开始根据父类id查询spit");
        Page<Spit> list=spitService.findByParentId(parentid,page,size);
        return new Result(true,StatusCode.OK,"根据父类查询成功",new PageResult<Spit>(list.getTotalElements(),list.getContent()));
    }

    @RequestMapping(value = "/thumbup/{spitId}",method = RequestMethod.PUT)
    public Result thumbup(@PathVariable String spitId){

        //111为用户id
        if (redisTemplate.opsForValue().get("spit_"+spitId+111)!=null){
            return  new Result(false,StatusCode.ERROR,"不能重复点赞");
        }
        redisTemplate.opsForValue().set("spit_"+spitId+111,1);
        spitService.thumbup(spitId);
        return  new Result(true,StatusCode.OK,"点赞成功");

    }



}
